<?php
/**
 * Created by PhpStorm.
 * Project: Parser Exist
 * File:    index.php
 * Date:    18.07.13
 * Time:    9:58
 * User:    azzaro.max@gmail.com
 */



include_once 'IEClass.php';


$site_url = 'http://exist.ua';

$db = new mysqli('avtopro.msgroup.ua', 'u_avtopro2', 'H1AuXxH6', 'avtopro');

$ie = new browser();
$ie2 = new browser();


$sql = "
    SELECT `id_search_product`, `oe`, `oe_search`, `name`, `manufacture`
    FROM `avtopro_search`
    WHERE `manufacture` IN ('SSANGYONG', 'Hyundai/KIA', 'Chevrolet')
    ORDER BY `manufacture`";

$result = $db->query($sql);
$i = 1;
while ($item = $result->fetch_object())
{
    $res = $db->query("SELECT `id` FROM `ex_app_links` WHERE `oe` = '{$item->oe}' AND (bad <> 1 OR bad IS NOT NULL)");
    if ($res->num_rows > 0) continue;
    $ie->navigate($site_url . '/price.aspx?pcode=' . $item->oe);
    if (is_null($ie->document->getElementById('lblError')))
    {
        print $item->oe . " - ";
        $element_id = $ie->document->getElementById('_0_');
        $href = null;
        if (is_null($element_id))
        {
            $trs =  $ie->document->getElementsByTagName('tr');
            foreach ($trs as $tr)
            {
                if (!is_null($tr->getAttribute('onclick')))
                {
                    $innerTEXT = strtolower($tr->children[0]->innerTEXT);
                    if (preg_match("/hyundai/", $innerTEXT) or preg_match("/general motors/", $innerTEXT) or preg_match("/ssang yong/", $innerTEXT))
                    {
                        $getpr = $tr->getAttribute('onclick');


                        preg_match_all("/\((.)+\)/", $getpr, $m);
                        $getpr = $m[0][0];

                        $lines = explode(',', $getpr);
                        $pid = str_ireplace(array("'", "(", ")"), '', $lines[0]);
                        $pid_href = $site_url . "/price.aspx?pid=" . $pid;

                        $ie2->navigate($pid_href);
                        if (is_object($ie2))
                        {
                            $element_id = $ie2->document->getElementById('_0_');
                            if (is_object($element_id->children[2]->children[0]))
                            {
                                $href = $element_id->children[2]->children[0]->getAttribute('href');
                            }
                            else
                            {
                                $href = null;
                            }

                        }
                    }
                }
            }
        }
        else
        {
            if (!is_null($element_id->children[2]->children[0]))
                $href = $element_id->children[2]->children[0]->getAttribute('href');
            else
                $href = null;
        }
        if (!is_null($href))
        {
            $link = $db->real_escape_string($site_url . $href);
            $oe = $db->real_escape_string($item->oe);
            $manufacture = $db->real_escape_string($item->manufacture);
            $db->query("INSERT INTO `ex_app_links` (`oe`, `link`, `manufacture`) VALUES ('$oe', '$link', '$manufacture')");
            print $i++ . ' from ' . $result->num_rows . ' - ' . $manufacture . ' - ' . $site_url .$href . "\n";
        }
        else
        {
            print "WTF: " . $item->oe . "\n";
            $oe = $db->real_escape_string($item->oe);
            $db->query("INSERT INTO `ex_app_links` (`oe`, `bad`) VALUES ('$oe', 1)");
        }
    }
    else
    {
        print "WTF: " . $item->oe . "\n";
        $oe = $db->real_escape_string($item->oe);
        $db->query("INSERT INTO `ex_app_links` (`oe`, `bad`) VALUES ('$oe', 1)");
    }

}







$ie = null;
$ie2 = null;






















